<?php

namespace App\Wxapp\Controllers;

use App\Store\Models\Account\SiteStore;
use App\Store\Models\Wxapp\Electronicchop;
use App\Store\Models\Wxapp\WarrantycertificateSetting;
use App\Store\Models\Wxapp\WxappCoupon;
use App\Store\Models\Wxapp\WxappCouponCodeList;
use App\Store\Models\Wxapp\WxappSetting;
use App\Store\Models\Wxapp\WxappSwimg;
use Illuminate\Http\Request;

/**
 * 公共配置相关
 */
class CommonController extends BaseController
{


    public function getcontactinfo(Request $request)
    {
        $store_id =$request->post('store_id')??0;

       // $info = WxappSetting::where([['cp_id', $this->cp_id]])->select(['contact_phone', 'contact_wxaccount'])->first();
        $info =SiteStore::query()
            ->where('id',$store_id)
            ->where('is_del',1)
            ->select(['contact_phone', 'wx_number'])
            ->first();
        $info=empty($info)?[]:$info->toArray();
        $info['contact_phone'] =$info['contact_phone']?? '';
        $info['contact_wxaccount'] =$info['wx_number']?? '';

        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => ['info' => $info]]);
    }

    public function getswimg(Request $request)
    {
        $list = WxappSwimg::getListByMem($request, $this->cp_id);
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $list]);
    }

    public function getcouponlist(Request $request)
    {
        $list = WxappCoupon::getListByMem($request, $this->cp_id);
        foreach ($list['list'] as &$val) {
            if ($val['store_ids'] == '') {
                $val['store_listinfo'] = SiteStore::where(['cp_id' => $this->cp_id])->select(['id', 'store_name'])->get()->toArray();
            } else {
                if (is_numeric($val['store_ids']) && intval($val['store_ids']) > 0) {
                    $val['store_listinfo'] = SiteStore::where(['id' => intval($val['store_ids']), 'cp_id' => $this->cp_id])->select(['id', 'store_name'])->get()->toArray();
                } else {
                    $val['store_listinfo'] = SiteStore::whereIn('id', explode(',', $val['store_ids']))->where(['cp_id' => $this->cp_id])->select(['id', 'store_name'])->get()->toArray();
                }
            }
        }
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $list]);
    }

    /**
     * 获取质保单配置
     */
    public function getwarrantycertificatesetting(Request $request)
    {
        $settting = WarrantycertificateSetting::query()
            ->where(['cp_id'=>$this->cp_id])
            ->first();
        $data = [
            'desc1' => $settting ? $settting->desc1 : '',
            'desc2' => $settting ? $settting->desc2 : ''
        ];
        $imgpath = Electronicchop::query()
            ->where([['status', 2], ['cp_id', $this->cp_id]])
            ->value('imgpath');
        $data['imgpath'] = !empty($imgpath) ? $imgpath : env('WXAPP_WARRANTYCERTIFICATE_IMG');


        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $data]);
    }

}
